<template>
  <!-- el-tree组件在封装的时候支持作用域插槽
          其中的data和node是由组件的开发者决定的 -->
  <el-row
    type="flex"
    justify="space-between"
    align="middle"
    style="height: 40px; width: 100%"
  >
    <el-col>
      <span>{{ treeNode.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <!-- 两个内容 -->
        <el-col>{{ treeNode.manager }}</el-col>
        <el-col>
          <!-- 下拉菜单 element -->
          <el-dropdown @command="operateDepts">
            <span> 操作<i class="el-icon-arrow-down" /> </span>
            <!-- 下拉菜单 -->
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item
                v-if="!isRoot"
                command="edit"
              >编辑部门</el-dropdown-item>
              <el-dropdown-item
                v-if="!isRoot"
                command="delete"
              >删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartments } from '@/api/departments'
export default {
  props: {
    treeNode: {
      type: Object,
      required: true
    },
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    operateDepts(val) {
      if (val === 'add') {
        // 通知父组件打开弹窗
        this.$emit('openDialog', this.treeNode)
      } else if (val === 'edit') {
        console.log('edit')
        this.$emit('editDialog', this.treeNode)
      } else if (val === 'delete') {
        this.$confirm('此操作将永久删除该节点, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
          await delDepartments(this.treeNode.id)
          // 通知父组件获取最新的数据
          this.$emit('delDep')
        }).catch(err => {
          console.log(err)
        })
        // // 可以使用await对$comfirm弹窗方法进行改造
        // const val = await this.$confirm('此操作将永久删除该节点, 是否继续?', '提示', {
        //   confirmButtonText: '确定',
        //   cancelButtonText: '取消',
        //   type: 'warning'
        // }).catch(err => err)
        // if (val === 'confirm') {
        //   await delDepartments(this.treeNode.id)
        // }
      }
    }
  }
}
</script>

<style scoped>
</style>
